//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include "ListCathCDDF.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <fstream>
#include <stdexcept>

#include <zlib.h>
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHLINE  5000
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
ListCathCDDF::ListCathCDDF () {

  this->namefile       = new String ();
  this->listDomainInfo = new TListE <CathCDDF> ();

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
ListCathCDDF::ListCathCDDF (const ListCathCDDF& listCathCDDF) {

  this->namefile       = new String (listCathCDDF.namefile);
  this->listDomainInfo = new TListE <CathCDDF> (listCathCDDF.listDomainInfo);

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
ListCathCDDF::ListCathCDDF (const ListCathCDDF* listCathCDDF) {

  this->namefile       = new String (listCathCDDF->namefile);
  this->listDomainInfo = new TListE <CathCDDF> (listCathCDDF->listDomainInfo);

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
ListCathCDDF::~ListCathCDDF () {
  
  if (this->namefile)       delete this->namefile;
  if (this->listDomainInfo) delete this->listDomainInfo;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void ListCathCDDF::Namefile (String* namefile){
  
  if (namefile) this->namefile->In(namefile);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void ListCathCDDF::ListDomainInfo (TListE <CathCDDF>* listDomainInfo){
  
  if (listDomainInfo) *(this->listDomainInfo) = *(listDomainInfo);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* ListCathCDDF::Namefile (void){
  
  return this->namefile;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
TListE <CathCDDF>* ListCathCDDF::ListDomainInfo (void) {
  
  return this->listDomainInfo;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool ListCathCDDF::LoadData (void) {
  
  if (!this->namefile || !this->namefile->Length()) return false;
  
  bool isGZFile = (this->namefile->Contain("gz"))?1:0;
  
  fstream file;
  gzFile  zipFile;
  
  char    buffer [LENGTHLINE + 1];
  String* data = new String ();
  
  if (isGZFile) zipFile = gzopen (this->namefile->Out(), "rb9");
  else          file.open (this->namefile->Out(), fstream::in);
  
  while (!((isGZFile)?gzeof(zipFile):file.eof())) {
    
    if (isGZFile) gzgets(zipFile, buffer, LENGTHLINE);
    else          file.getline(buffer, LENGTHLINE);
    
    if (!strlen(buffer) || (buffer[0] == '#')) continue;
    
    if (!strncmp(buffer, "//", strlen("//"))) {
      
      CathCDDF* newCathCDDF = new CathCDDF ();
      
      if (newCathCDDF->ReadFromString(data)) 
        this->listDomainInfo->Add(newCathCDDF);
      else 
        delete newCathCDDF;
      
      data->In("");
      
      continue;
      
    }
    
    data->Add(buffer);
    data->Add("\n");
    
  }
  
  if (isGZFile) gzclose(zipFile);
  else          file.close();
  
  if (data)     delete data;
   
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
ListCathCDDF ListCathCDDF::operator= (const ListCathCDDF& listCathCDDF) {
  
  this->namefile->In(listCathCDDF.namefile);
  
  *(this->listDomainInfo) = *(listCathCDDF.listDomainInfo);  
  
  return *this;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
